package com.quicklife.action.android;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.jspsmart.upload.SmartUpload;
import com.quicklife.service.impl.AboutMeServiceImpl;
import com.quicklife.service.inter.AboutMeService;
import com.quicklife.util.StringUtil;

public class FileUploadAction extends HttpServlet {
	private ServletConfig config = getServletConfig();
	private String FileName = null;
	private String sPath = "/upload";

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String temp = request.getSession().getServletContext().getRealPath("/")
				+ "upload"; // 临时目录
		String loadpath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "upload"; // 上传文件存放目录
		DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
		fileItemFactory.setSizeThreshold(1024 * 1024);// 设置最多只允许在内存中存储的数据,单位:字节
		fileItemFactory.setRepository(new File(temp));// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				fileItemFactory);
		servletFileUpload.setHeaderEncoding("ISO8859_1");
		// servletFileUpload.setSizeMax(sizeMax)// 设置允许用户上传文件大小,单位:字节
		// 开始读取上传信息
		List<FileItem> fileItems = null;

		try {
			fileItems = servletFileUpload.parseRequest(request);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Iterator<FileItem> iter = fileItems.iterator(); // 依次处理每个上传的文件
		while (iter.hasNext()) {
			FileItem item = iter.next();// 忽略其他不是文件域的所有表单信息
			if (!item.isFormField()) {
				String name = item.getName();// 获取上传文件名,包括路径

				long size = item.getSize();
				if ((name == null || name.equals("")) && size == 0) {
					continue;
				}
				//生成唯一名称
				int point = name.indexOf(".");
				name = StringUtil.getUUID()
						+ name.substring(point, name.length());

				File fNew = new File(loadpath, name);
				try {
					item.write(fNew);
				} catch (Exception e) {
					e.printStackTrace();
				}

			} else// 取出不是文件域的所有表单信息
			{
				// String fieldvalue = item.getString();
				// 如果包含中文应写为：(转为UTF-8编码)
				String fieldvalue = new String(item.getString().getBytes(
						"ISO8859_1"), "UTF-8");
			}
		}

		// 返回结果
		out.print("111111111111");

		out.flush();
		out.close();
	}

}
